/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mzm.ui.admin;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JScrollPane;

import com.mzm.api.RecommendationDto;

/**
 * 
 * @author daniel
 */
public class RecommendationsPanel extends javax.swing.JPanel {

	private List<RecommendationEditorPanel> editors = new ArrayList<>();

	private AdminContext adminContext;

	/**
	 * Creates new form RecommendationsPanel
	 */
	public RecommendationsPanel() {
		initComponents();
	}

	public void setAdminContext(AdminContext adminContext) {
		this.adminContext = adminContext;
	}

	public void clearRecommendations() {
		recommendationsTabbedPane.removeAll();
		editors.clear();
	}

	public void showRecommendation(String title, RecommendationDto recommendation) {
		JScrollPane scrollPane = new JScrollPane();
		RecommendationEditorPanel recommendationEditorPanel = new RecommendationEditorPanel();
		recommendationEditorPanel.setRecommendationDto(recommendation);
		scrollPane.setViewportView(recommendationEditorPanel);
		recommendationsTabbedPane.addTab(title, recommendationEditorPanel);
		editors.add(recommendationEditorPanel);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        recommendationsTabbedPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        deleteJB = new javax.swing.JButton();
        saveJB = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());
        add(recommendationsTabbedPane, java.awt.BorderLayout.CENTER);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(60, 40));

        deleteJB.setText("מחק");
        deleteJB.setToolTipText("מוחק את ההמלצה הנוכחית");
        deleteJB.setPreferredSize(new java.awt.Dimension(60, 30));
        deleteJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteJBActionPerformed(evt);
            }
        });
        jPanel1.add(deleteJB);

        saveJB.setText("שמור");
        saveJB.setToolTipText("מבצע שמירה של כל ההמלצות");
        saveJB.setPreferredSize(new java.awt.Dimension(60, 30));
        saveJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveJBActionPerformed(evt);
            }
        });
        jPanel1.add(saveJB);

        add(jPanel1, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

	private void deleteJBActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_deleteJBActionPerformed
		RecommendationDto recommendationDto = getSelectedRecommendation();
		adminContext.getMzmAdminFacade().deleteRecommendation(recommendationDto.getId());
		adminContext.refreshRecommendations();
	}// GEN-LAST:event_deleteJBActionPerformed

	private RecommendationDto getSelectedRecommendation() {
		int selectedIndex = recommendationsTabbedPane.getSelectedIndex();
		RecommendationDto recommendationDto = editors.get(selectedIndex).getRecommendationDto();
		return recommendationDto;
	}

	private void saveJBActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveJBActionPerformed
		RecommendationDto recommendationDto = getSelectedRecommendation();
		if (recommendationDto.getId() != null) {
			adminContext.getMzmAdminFacade().updateRecommendation(recommendationDto);
		} else {
			adminContext.getMzmAdminFacade().addRecommendation(
					adminContext.getSeletedProductInfoId(), recommendationDto);
		}
	}// GEN-LAST:event_saveJBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteJB;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane recommendationsTabbedPane;
    private javax.swing.JButton saveJB;
    // End of variables declaration//GEN-END:variables
}
